/**
 * 预约建档初始化
 */
var RevisitStatisticsTable = {
    id: "revisitStatisticsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
/*
*  选择跟进人
*/
RevisitStatisticsTable.getArchiving = function () {
    var index = layer.open({
        type: 2,
        title: '选择建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

/**
 * 初始化表格的列
 */
RevisitStatisticsTable.initColumn = function () {
    var columns = [
        {
            field: 'selectItem', radio: true
        },
        {
            title: '', field: 'id', visible: false, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '预约号', field: 'reservationsNum', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                var styleCss = "";
                if (row.status == "到店") {
                    styleCss = "style='color: green'";
                    return "<a href='javascript:;' " + styleCss + " onclick='ReservationsRecordTable.reservationsArrives(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>";
                } else {
                    if (row.department == "电销咨询" || row.department == "公共池") {
                        return "<a href='javascript:;' onclick='ReservationsRecordTable.reservationsNotDetails(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>"
                    } else {
                        return "<a href='javascript:;' onclick='ReservationsRecordTable.reservationsNotDetail(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>"
                    }
                }
            }
        },

        {
            title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 150,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '预约门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '到店状态', field: 'status', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '回访状态', field: 'statuss', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '回访人', field: 'revisit', align: 'center', valign: 'middle',width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '回访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 200,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '回访内容', field: 'revisitContent', align: 'center', valign: 'middle',width: 200,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle',width: 200,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 200,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '初诊时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 200,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value.substring(0, 19) + "</span>"
                } else {
                    return value.substring(0, 19);
                }
            }
        },

        {
            title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '部门', field: 'department', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 200,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value.substring(0, 19) + "</span>"
                } else {
                    return value.substring(0, 19);
                }
            }
        },
        {
            title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                var sexValue = "";
                if (value == 1) {
                    sexValue = "男";
                } else if (value == 2) {
                    sexValue = "女"
                } else {
                    sexValue = "未知"
                }
                if (row.status == "到店") {
                    return "<span style='color: green'>" + sexValue + "</span>"
                } else {
                    return sexValue;
                }
            }
        },
        {
            title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '成交状态',
            visible: false,
            field: 'dealStatus',
            align: 'center',
            valign: 'middle',
            sortable: true,
            width: 120,
            formatter: function (value, row, index) {
                var styleCss = "";
                if (row.status == "到店") {
                    styleCss = "style='color: green'";
                }
                if (value == "已成交") {
                    return "<a href='javascript:;' " + styleCss + " onclick='ReservationsRecordTable.opendealStatus(" + '"' + row.id + '"' + ")'>[已成交]</a>"
                } else {
                    return "<span " + styleCss + ">" + value + "</span>"
                }
            }
        },
        {
            title: '预约病种',
            field: 'appointmentPayService',
            align: 'center',
            valign: 'middle',
            sortable: true,
            width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '访问病种', field: 'accessDisease', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
    ]
    return columns;
};

var Time = new Date();
var Year = Time.getFullYear();
var Month = Time.getMonth()+1;
var Day = Time.getDate();
if(Month < 10){
    Month = "0" + Month
}
if(Day < 10){
    Day = "0" + Day
}
var newDate = Year + "-" + Month + "-" + Day;  //今天
var newMonth = Year + "-" + Month;  //本月

var time1 = new Date();
time1.setDate(time1.getDate()-1);
var year1 = time1.getFullYear();
var month1 = time1.getMonth()+1;
var day1 = time1.getDate();
if(month1 < 10){
    month1 = "0" + month1
}
if(day1 < 10){
    day1 = "0" + day1
}
var yestDate = year1 + "-" + month1 + "-" + day1;  //昨天

var time2 = new Date();
time2.setMonth(time2.getMonth() - 1);
var year2 = time2.getFullYear();
var month2 = time2.getMonth()+1;
if(month2 < 10){
    month2 = "0" + month2
}
var lastMonth =  year2 + "-" + month2 ; //上月

/**
 * 查询预约客户列表
 */
RevisitStatisticsTable.search = function () {
    RevisitStatisticsTable.table.refresh({query: RevisitStatisticsTable.formParams()});
    selectCount();
}


RevisitStatisticsTable.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};


RevisitStatisticsTable.getData = function () {
    RevisitStatisticsTable.search();
}


/**
 *  新 市场预约表单查询提交参数
 * @returns {{}}
 */
RevisitStatisticsTable.formParams = function () {
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val(); //门诊
    queryData['accessDisease'] = $("#accessDisease").val();  //访问病种
    queryData['startFilingTime'] = $("#startFilingTime").val();  //建档时间
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();  //回访时间
    queryData['endtRevisitTime'] = $("#endtRevisitTime").val();
    queryData['revisit'] = $("#follower").val();     //回访人
    var fieldTc = $('#fieldTc').val();
    var str2= "";
    if (fieldTc != null && fieldTc != "") {
        for (var i = 0; i < fieldTc.length; i++) {
            if (i == (fieldTc.length - 1)) {
                str2 += fieldTc[i];
            } else {
                str2 += fieldTc[i] + ",";
            }
        }
    }
    queryData['fieldTc'] = str2;
    //JSON.stringify() 输出对象的全部内容
    // alert(JSON.stringify(queryData));
    return queryData;
};

//选择回访人
RevisitStatisticsTable.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};


/**
 *  导出
 */
RevisitStatisticsTable.reservationsExport = function () {
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val(); //门诊
    queryData['department'] = $("#department").val(); //  部门
    queryData['reach'] = $("#reach").val().trim();  //姓名   电话   微信  预约号
    queryData['appointmentPayService'] = $("#appointmentPayService").val();  //预约项目
    queryData['archiving'] = $("#archiving").val();   //建档人
    queryData['follower'] = $("#follower").val();
    queryData['accessDisease'] = $("#accessDisease").val();  //访问病种
    queryData['oneChannel'] = $("#oneChannel").val();  //一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();    //二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();    //三级渠道
    queryData['wechatNumber'] = $("#wechatNumber").val();   //微信
    queryData['statuss'] = $("#statuss").val();   //状态
    queryData['status'] = $("#status").val();   //状态
    queryData['startFilingTime'] = $("#startFilingTime").val();  //建档时间
    queryData['endFilingTime'] = $("#endFilingTime").val();
    var pageType = $("#pageType").val();
    if (pageType == '0') {
        //建档
        queryData['startFilingTime'] = $("#startFilingTime").val();  //建档时间
        queryData['endFilingTime'] = $("#endFilingTime").val();
    } else if (pageType == '1') {
        //已到
        queryData['startFirstVisitDate'] = $("#startFirstVisitDate").val();  //建档时间
        queryData['endFirstVisitDate'] = $("#endFirstVisitDate").val();
    } else if (pageType == '2') {
        //未到
        queryData['startAppointmentArrivalTime'] = $("#startAppointmentArrivalTime").val();  //建档时间
        queryData['endAppointmentArrivalTime'] = $("#endAppointmentArrivalTime").val();
    }
    queryData['pageType'] = $("#pageType").val();   // 界面参数
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/reservation/reservationsExport?' + param;
}

/**
 * 重置
 */
RevisitStatisticsTable.resetSearch = function () {
    $("#outpatientName").val("");
    $("#accessDisease").val("");
    $("#fieldTc").val("");
    $("#follower").val("");
    $("#startFilingTime").val(newDate + " " + "00:00:00");
    $("#endFilingTime").val(newDate + " " + "23:59:59");
    $("#startRevisitTime").val(newDate + " " + "00:00:00");
    $("#endRevisitTime").val(newDate + " " + "23:59:59");
    RevisitStatisticsTable.search();
};

/**
 * 统计建档，已到，未到 人数
 */
function selectCount() {
    //获取门诊、部门、建档人参数
    var param1 = $("#outpatientName").val();  //门诊
    var param2 = $("#accessDisease").val();   //访问病种
    var param3 = $("#fieldTc").val();  //TC
    var param4 = $("#follower").val(); //回访人
    var param5 = $("#startFilingTime").val();  //建档时间
    var param6 = $("#endFilingTime").val();  //
    var param7 = $("#startRevisitTime").val();  //回访时间
    var param8 = $("#endRevisitTime").val();  //回访时间
    //发送ajax请求后台查询统计信息，赋值到对应的地方
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/returnVisitStatistics/tongji",
        data: {
            param1: param1, param2: param2, param3: param3,
            param4: param4, param5: param5, param6: param6,
            param7: param7, param8: param8
        },
        dataType: "json",
        success: function (data) {
            console.log("+++++++++"+data);
            $("#huifangNum").html(data);
        }
    });
};

//页面初始化
$(function () {
    //默认回访时间
    document.getElementById("startRevisitTime").value = newDate + " " + "00:00:00";
    document.getElementById("endRevisitTime").value = newDate + " " + "23:59:59";
    //默认建档时间
    document.getElementById("startFilingTime").value = newDate + " " + "00:00:00";
    document.getElementById("endFilingTime").value = newDate + " " + "23:59:59";
    //加载表格数据
    var defaultColunms = RevisitStatisticsTable.initColumn();
    var table = new BSTable(RevisitStatisticsTable.id, "/returnVisitStatistics/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(RevisitStatisticsTable.formParams());
    RevisitStatisticsTable.table = table.init();
    //初始化统计数据
    RevisitStatisticsTable.search();

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">全部</option>';
        console.log(data);
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientName').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if (seleteooutpatientName != "") {
            $("#outpatientName option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");

    $.post(Feng.ctxPath + "/tc/getList", function (data) {
        for (var i = 0; i < data.length; i++) {
            $('#fieldTc').append("<option value=" + data[i].tcName + ">" + data[i].tcName + "</option>");
        }
        $('#fieldTc').selectpicker('refresh');
        $('#fieldTc').selectpicker('render');
    }, "json");

    //采用定时器监听 时间内容是否变化
    var oldStartFilingTime = $("#startFilingTime").val();
    var oldEndFilingTime= $("#endFilingTime").val();

    var oldStartRevisitTime = $("#startRevisitTime").val();
    var oldEndRevisitTime = $("#endRevisitTime").val();

    var oldFollowerValue = $("#follower").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#startFilingTime").val() != oldStartFilingTime || $("#endFilingTime").val() != oldEndFilingTime) {//如果时间发生变化，就触发查询事件
                RevisitStatisticsTable.getData();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartFilingTime = $("#startFilingTime").val();
                oldEndFilingTime = $("#endFilingTime").val();
            }
            if ($("#startRevisitTime").val() != oldStartRevisitTime || $("#endRevisitTime").val() != oldEndRevisitTime) {//如果时间发生变化，就触发查询事件
                RevisitStatisticsTable.getData();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartRevisitTime = $("#startRevisitTime").val();
                oldEndRevisitTime = $("#endRevisitTime").val();
            }
            if (oldFollowerValue != $("#follower").val()) {
                RevisitStatisticsTable.getData();
                oldFollowerValue = $("#follower").val();//每次判断为修改后把最新的文本框的值给原有变量，为下一次循坏执行方法体里面的判断做准备
            }
        }, 500);

});
